package 第5章多线程编程作业.Work1.Work1;

public class TestAccount implements Runnable{
    private Account acct=new Account();

    public void run() {
        for (int i = 0; i <10 ; i++) {
            makeWithdraw(100);
            if (acct.getBalance()<0){
                System.out.println("账户透支了！");
            }
        }
    }
    private void makeWithdraw(int amt){
      synchronized (acct){ if (acct.getBalance()>amt){
            System.out.println(Thread.currentThread().getName()+"准备取款100元，账户余额为"+acct.getBalance());
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            acct.withdraw(amt);
            System.out.println(Thread.currentThread().getName()+"完成取款，账户余额为："+acct.getBalance()+"元");
        }
        else{
            System.out.println(Thread.currentThread().getName()+"准备取款，"+"账户余额不足");
        }
    }}
}
